package com.example.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * Created with IntelliJ IDEA.
 * Description:定义返回结果实体类
 * User: bjc
 * Date: 2025-09-02
 * Time: 19:42
 */
@Data
public class AppResult<T> {

    //描述统一格式中的数据
    @JsonInclude(JsonInclude.Include.ALWAYS) //表示不论任何情况下，对应属性都参与序列化
    private T data;
    //描述统一格式中的编码，用于区分操作
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private Integer code;
    //描述统一格式中的错误信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String errMsg;

    //定义有参构造方法
    public AppResult(Integer code, String errMsg) {
        this.code = code;
        this.errMsg = errMsg;
    }
    // 完整构造函数
    public AppResult(T data, Integer code, String errMsg) {
        this.data = data;
        this.code = code;
        this.errMsg = errMsg;
    }

    //业务成功时方法
    public static <T> AppResult<T> success() {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> AppResult<T> success(String message) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message);
    }
    public static <T> AppResult<T> success(T data) {
        return new AppResult<>(data,ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }
    public static <T> AppResult<T> success(T data, String message) {
        return new AppResult<>(data, ResultCode.SUCCESS.getCode(), message);
    }
    //业务失败时方法
    public static <T> AppResult<T> failed() {
        return new AppResult<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }
    public static <T> AppResult<T> failed(String message) {
        return new AppResult<>(ResultCode.FAILED.getCode(), message);
    }
    public static <T> AppResult<T> failed(ResultCode resultCode) {
        return new AppResult<>(resultCode.getCode(), resultCode.getMessage());
    }
}
